package com.tencreat.service.mall.impl;

import com.tencreat.common.core.domain.entity.MallMessage;
import com.tencreat.service.mall.IMallMessageService;
import com.tencreat.socket.NettyWebSocket;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.connection.MessageListener;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Component;

/**
 * 描述信息ImMessageListener
 *
 * @author zixin
 * @company 深圳腾创互联科技有限公司
 * @link https://tencreat.com
 * @date 2022/10/26 15:30
 */

@Slf4j
@Component
@RequiredArgsConstructor
public class ImMessageListener implements MessageListener {

    private final RedisTemplate redisTemplate;
    private final IMallMessageService mallMessageService;
    private final ThreadPoolTaskExecutor threadPoolTaskExecutor;

    @Override
    public void onMessage(Message message, byte[] pattern) {
        RedisSerializer<MallMessage> redisSerializer = redisTemplate.getValueSerializer();
        MallMessage mallMessage = redisSerializer.deserialize(message.getBody());
        log.info("接收到的消息是："+ mallMessage);
        // 消息处理
        threadPoolTaskExecutor.execute(() -> mallMessageService.messageListHandling(mallMessage));
    }
}
